package com.lpc.utilclasstest;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

/**
 * @ Author     ：v_lipengcheng
 * @ Date       ：Created in 15:11 2018/12/25
 * @ Description：
 */
public class DisplayUtil {

    private static final String TAG = "lipc0113";

    public static void printDisplayMetrics(Activity activity) {
        DisplayMetrics displayMetrics = getDisplayMetrics(activity);
        Log.v(TAG, "---printDisplayMetrics---" +
                "widthPixels=" + displayMetrics.widthPixels
                + ", heightPixels=" + displayMetrics.heightPixels
                + ", density=" + displayMetrics.density
                + ", densityDpi=" + displayMetrics.densityDpi
                + ", xdpi=" + displayMetrics.xdpi
                + ", ydpi=" + displayMetrics.ydpi);

        // 这样可以计算屏幕的物理尺寸
        float width2 = (displayMetrics.widthPixels / displayMetrics.xdpi)
                * (displayMetrics.widthPixels / displayMetrics.xdpi);
        float height2 = (displayMetrics.heightPixels / displayMetrics.ydpi)
                * (displayMetrics.widthPixels / displayMetrics.xdpi);
        Log.v("lipc0113", "手机屏幕物理尺寸 ---" + Math.sqrt(width2 + height2));
    }

    public static void printDisplayMetrics(Context context) {
        DisplayMetrics displayMetrics = getDisplayMetrics(context);
        Log.v(TAG, "---printDisplayMetrics---" +
                "widthPixels=" + displayMetrics.widthPixels
                + ", heightPixels=" + displayMetrics.heightPixels
                + ", density=" + displayMetrics.density
                + ", densityDpi=" + displayMetrics.densityDpi
                + ", xdpi=" + displayMetrics.xdpi
                + ", ydpi=" + displayMetrics.ydpi);

        // 这样可以计算屏幕的物理尺寸
        float width2 = (displayMetrics.widthPixels / displayMetrics.xdpi)
                * (displayMetrics.widthPixels / displayMetrics.xdpi);
        float height2 = (displayMetrics.heightPixels / displayMetrics.ydpi)
                * (displayMetrics.widthPixels / displayMetrics.xdpi);
        Log.v("lipc0113", "手机屏幕物理尺寸 ---" + Math.sqrt(width2 + height2));
    }

    private static DisplayMetrics getDisplayMetrics(Activity activity) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //4.2开始有虚拟导航栏，增加了该方法才能准确获取屏幕高度(包含了虚拟导航栏的高度)
            activity.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
        } else {
            activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            //displayMetrics = activity.getResources().getDisplayMetrics();//或者该方法也行
        }
        return displayMetrics;
    }

    private static DisplayMetrics getDisplayMetrics(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);
        } else {
            windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        }
        return displayMetrics;
    }

}
